<?php
class quark{
	protected $id,$job,$ressourceLink,$thresholdCount,$feedbackCount,$createdAt,$updatedAt;
	const RESSOURCE_MAX_SIZE = 350000; 
	
	function __construct($id,$jobid,$ressourceLink,$thresholdCount,$feedbackCount,$createdAt,$updatedAt){
		$this->id = $id;
		$this->job = job::getJobById($jobid);
		$this->ressourceLink = $ressourceLink;
		$this->thresholdCount = $thresholdCount;
		$this->$feedbackCount = $feedbackCount;
		$this->createdAt = $createdAt;
		$this->updatedAt = $updatedAt;
	}
	
	/*GETTTERS*/
	public function getId(){
		return $this->id;	
	}
	
	public function getJob(){
		return $this->job;	
	}

	public function getRessourceLink(){
		return $this->ressourceLink;	
	}

	public function getThresholdCount(){
		return $this->thresholdCount;	
	}

	public function getFeedbackCount(){
		return $this->feedbackCount;	
	}
	

	public function getResponseCount(){
		$conn = new dbmodifier();
		$userid = $conn->db_connection->real_escape_string($userid);	
		$jobid = $conn->db_connection->real_escape_string($jobid);	
		$query = "SELECT COUNT( * )  AS 'response_count' FROM quark".
				" INNER JOIN quarkresponse ON quarkresponse.quark_id = quark.id WHERE quark.id =".$this->id;
		$result = $conn->updateSQL($query);
		$conn->execute();
		$row = $result->fetch_assoc();
		return $row['response_count'];
	}
	
	public function getCreatedAt(){
		return $this->createdAt;	
	}

	public function getUpdatedAt(){
		return $this->updatedAt;	
	}

	public function setJob($var){
		 $this->job= $var;
	}

	public function setRessourceLink($var){
		 $this->ressourceLink= $var;
	}

	public function setThresholdCount($var){
		 $this->thresholdCount= $var;
	}
	
	public function setFeedbackCount($var){
		 $this->feedbackCount= $var;
	}

	public function getPathToRessource(){
		$path = self::getFolderPath($this->getJob()->getUser()->getId(),$this->getJob()->getId());
		$path = $path."\\".$this->getRessourceLink();
		return $path;
	}
	public static function getFolderPath($userid,$jobid){
		$u = substr(md5($userid),1,7);
		$j = substr(md5($jobid),1,7);
		$p = "resources\\".$u."\\".$j;
		return $p;
		
	}

	public static function getQuarkByJobIdAndNotQuarkId($qid,$jid){
		$conn = new dbmodifier();
		$userid = $conn->db_connection->real_escape_string($userid);	
		$jobid = $conn->db_connection->real_escape_string($jobid);	
		$query = "SELECT *, RAND() AS 'ord' FROM quark WHERE quark.id <> ".$qid." AND quark.job_id = ".
					$jid." ORDER BY ord LIMIT 1";
		$result = $conn->updateSQL($query);
		$conn->execute();
		if(($result->num_rows)>0){
			$row = $result->fetch_assoc();			
			$var = new quark($row['id'],$row['job_id'],$row['ressource_link'],$row['threshold_count']
					,$row['feedback_count'],$row['created_at'],$row['updated_at']);
		}else{
			$var = NULL;
		}
		return $var;		
		return $var;	
	
	}
	public static function getNextQuarkByUserIdAndByJobId($userid,$jobid){
		$conn = new dbmodifier();
		$userid = $conn->db_connection->real_escape_string($userid);	
		$jobid = $conn->db_connection->real_escape_string($jobid);	
		$query = "SELECT q.id, q.job_id, q.ressource_link, q.threshold_count, q.feedback_count,".
				" UNIX_TIMESTAMP(q.created_at) AS created_at, UNIX_TIMESTAMP(q.updated_at) AS updated_at,".
				" COUNT(*) AS reponse_count FROM quark AS q LEFT OUTER JOIN quarktransmitted AS qt".
				" ON q.id = qt.quark_id AND qt.user_id = ".$userid." LEFT OUTER JOIN 	quarkresponse AS qr".
				" ON q.id = qr.quark_id WHERE qt.quark_id IS NULL AND q.job_id = ".$jobid." GROUP BY".
				" qr.quark_id ORDER BY 8 DESC LIMIT 1";
		$result = $conn->updateSQL($query);
		$conn->execute();
		if(($result->num_rows)>0){
			$row = $result->fetch_assoc();			
			$var = new quark($row['id'],$row['job_id'],$row['ressource_link'],$row['threshold_count']
					,$row['feedback_count'],$row['created_at'],$row['updated_at']);
		}else{
			$var = NULL;
		}
		return $var;		
		return $var;
	}
	
	public static function getQuarkByJobId($jobid){
		$conn = new dbmodifier();
		$userid = $conn->db_connection->real_escape_string($userid);
		$query = "SELECT id, job_id, ressource_link, threshold_count, feedback_count,".
					" UNIX_TIMESTAMP(created_at) AS 'created_at',".
					" UNIX_TIMESTAMP(updated_at) AS 'updated_at' FROM QUARK WHERE job_id =".$jobid."";
		$result = $conn->updateSQL($query);
		$conn->execute();
		return $result;	
	}
	
	public static function getquarkById($id){
		$conn = new dbmodifier();
		$id = $conn->db_connection->real_escape_string($id);
		$query = "SELECT id, job_id, ressource_link, threshold_count, feedback_count, ".
					" UNIX_TIMESTAMP(created_at) AS 'created_at',".
					" UNIX_TIMESTAMP(updated_at) AS 'updated_at' FROM QUARK WHERE id =".$id."";
		$result = $conn->updateSQL($query);
		$conn->execute();
		if(($result->num_rows)>0){
			$row = $result->fetch_assoc();			
			$var = new quark($id,$row['job_id'],$row['ressource_link'],$row['threshold_count']
					,$row['feedback_count'],$row['created_at'],$row['updated_at']);
		}else{
			$var = NULL;
		}
		return $var;			
	}

	public static function createquark($jobid,$ressourceLink,$thresholdCount,$feedbackCount,$solved){
		$conn = new dbmodifier();
		$jobid = $conn->db_connection->real_escape_string($jobid);
		$ressourceLink = $conn->db_connection->real_escape_string($ressourceLink);
		$thresholdCount = $conn->db_connection->real_escape_string($thresholdCount);
		$feedbackCount = $conn->db_connection->real_escape_string($feedbackCount);
		$query = "INSERT INTO quark (job_id,ressource_link,threshold_count,feedback_count,created_at,updated_at) " .
				 " VALUES ('".$jobid."','".$ressourceLink."','".
				 $thresholdCount."','".$feedbackCount."','FROM_UNIXTIME(".time()."),FROM_UNIXTIME(".time()."))";
		$id = $conn->updateSQLKey($query);
		$conn->execute();
		$var = new quark($id,$jobid,$ressourceLink,$thresholdCount,$feedbackCount,time(),time());
		return $var;		
	}
	
	public function save(){
		$conn = new dbmodifier();
		$jobid = $conn->db_connection->real_escape_string($this->job->getId());
		$ressourceLink = $conn->db_connection->real_escape_string($this->ressourceLink);
		$thresholdCount = $conn->db_connection->real_escape_string($this->thresholdCount);
		$feedbackCount = $conn->db_connection->real_escape_string($this->feedbackCount);	
		$query = "UPDATE quark SET job_id='".$jobid."'";
		$query = $query.", ressource_link='".$ressourceLink."'";
		$query = $query.", threshold_count='".$thresholdCount."'";
		$query = $query.", feedback_count='".$feedbackCount."'";
		$query = $query.", updated_at= FROM_UNIXTIME(".time().")";		
		$query = $query." WHERE id=".$this->id;
		$conn->updateSQL($query);
		$conn->execute();	
		$this->updated_at = time();		
	}
	
	public function delete(){
		//remove the ressources
		$path = self::getFolderPath($this->getJob()->getUser()->getId(),$this->getJob()->getId());
		$path = $path."\\".$this->getRessourceLink();
		unlink($path);
		$conn = new dbmodifier();
		$query = "DELETE FROM quark WHERE id=".$this->id;
		$result = $conn->updateSQL($query);
		$conn->execute();
		return $result;		
	}

	public static function deleteQuarkByJobId($id){
		$results =self::getQuarkByJobId($id);
		$job = job::getJobById($id);
		while($row = $results->fetch_assoc()){
			$path = self::getFolderPath($job->getUser()->getId(),$id);	
			$path = $path."\\".$row['ressource_link'];
			unlink($path);
		}
		$conn = new dbmodifier();
		$query = "DELETE FROM quark WHERE job_id=".$id;
		$result = $conn->updateSQL($query);
		$conn->execute();
		return $result;		
	}
}
?>